package com.stress.testing.plugin.es.interceptor;

import com.stress.testing.common.StressTesting;
import com.stress.testing.common.TraceContextHolder;
import com.stress.testing.common.interceptor.AbstractInterceptor;
import com.stress.testing.plugin.es.strategy.RequestIndexRenameStrategy;
import org.elasticsearch.action.ActionRequest;

import java.lang.reflect.Method;

public class RestHighLevelClientInterceptor extends AbstractInterceptor {
    @Override
    public void before(Method method, Object[] args, Object zuper, Object thiz) {
        if (!StressTesting.enable(getAgentConfig(), getPluginConfig(), TraceContextHolder.getTraceContext())) {
            return;
        }

        if (args == null || args.length == 0) {
            return;
        }

        Object arg = args[0];
        if (arg == null || !(arg instanceof ActionRequest)) {
            return;
        }

        RequestIndexRenameStrategy.getForClass(arg.getClass(), getAgentConfig(), getPluginConfig()).renameIndex(arg);
    }
}
